package com.learner.ucenter.controller.api;

import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.common.util.JwtInfo;
import com.learner.common.util.JwtUtils;
import com.learner.pojo.user.vo.DelGoodFriendRequestVo;
import com.learner.pojo.user.vo.MyFriendListResultVo;
import com.learner.pojo.user.vo.RecentConversationVo;
import com.learner.pojo.user.vo.SingleRecentConversationResultVo;
import com.learner.ucenter.service.GoodFriendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ice2020x
 * @Date: 2023/2/27
 * @Description: 好友控制器
 **/
@Slf4j
@RestController
@RequestMapping("/api/ucenter/goodFriend")
public class GoodFriendController {

    @Autowired
    private GoodFriendService goodFriendService;

    /**
     * 获取我的好友列表
     *
     * @param request request
     * @return MyFriendListResultVo 我的好友
     */
    @GetMapping("/getMyFriendsList")
    public List<MyFriendListResultVo> getMyFriendsList(HttpServletRequest request) {
        JwtInfo infoByJwtToken = JwtUtils.getInfoByJwtToken(request);
        List<MyFriendListResultVo> myFriendsList = goodFriendService.getMyFriendsList(infoByJwtToken.getUserId());
        log.info("我的好友列表为：" + myFriendsList);
        return myFriendsList;
    }

    /**
     * 根据id列表查询数据，传入的数据为用户id 以及一个最近的id的列表，没有做最近逻辑的判断
     *
     * @param recentConversationVo 参数
     * @return 结果
     */
    @PostMapping("/recentConversationList")
    public List<SingleRecentConversationResultVo> getRecentConversationList(@RequestBody RecentConversationVo recentConversationVo) {
        List<SingleRecentConversationResultVo> resultVoList = goodFriendService.getRecentConversation(recentConversationVo);
        log.info("根据id列表查询数据，传入的数据为用户id：{} 以及一个最近的id的列表：{}", recentConversationVo.getUserId(), recentConversationVo.getRecentFriendIds());
        return resultVoList;
    }

    /**
     * 删除好友
     *
     * @param requestVo 参数
     */
    @DeleteMapping("/deleteGoodFriend")
    public void deleteGoodFriend(@RequestBody DelGoodFriendRequestVo requestVo, HttpServletRequest request) {
        JwtInfo infoByJwtToken = JwtUtils.getInfoByJwtToken(request);
        String userId = infoByJwtToken.getUserId();
        if (!userId.equals(requestVo.getUserM())) {
            //不是本人，非法操作
            throw ExceptionUtil.createException(RestException.class, 1000018);
        }
        goodFriendService.deleteFriend(requestVo);
    }
}
